using System;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Unity;
using Ausy.Silverlight.VideoPlayer.Infrastructure.Common;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Ausy.Silverlight.VideoPlayer.Common.Configuration;

namespace Ausy.Silverlight.VideoPlayer.MediaModule.ViewParts
{
    /// <summary>
    /// Cette classe représente le Vue Model pour le Player
    /// </summary>
    public class PlayerViewModel : ViewModel<IPlayerView>, IPlayerViewModel
    {
        #region ScreencastCollection Property

        /// <summary>
        /// Property screencastCollection
        /// </summary>
        private ObservableCollection<IScreencastViewModel> screencastCollection;

        /// <summary>
        /// Property ScreencastCollection
        /// </summary>
        public ObservableCollection<IScreencastViewModel> ScreencastCollection
        {
            get { return screencastCollection; }
            set
            {
                if (screencastCollection != value)
                {
                    screencastCollection = value;
                    RaisePropertyChanged(() => this.ScreencastCollection);
                }
            }
        }

        #endregion
        
        #region ScreecastIndex Property
        /// <summary>
        /// Property screecastIndex
        /// </summary>
        private int screecastIndex;

        /// <summary>
        /// Property ScreecastIndex
        /// </summary>
        public int ScreecastIndex
        {
            get { return screecastIndex; }
            set
            {
                if (screecastIndex != value)
                {
                    screecastIndex = value;
                    RaisePropertyChanged(() => this.ScreecastIndex);
                }
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        /// <param name="container">Container Unity pour instanciation des type d'objet</param>
        public PlayerViewModel(IUnityContainer container)
            : base(container)
        {

        }

        #endregion

        #region Initialize Method

        /// <summary>
        /// Cette méthode permet l'initialisation du ViewModel avant que le DataContext soit affecté à la vue
        /// </summary>
        protected override void Initialize()
        {
            // Initialisation des données
            this.ScreecastIndex = 0;
            this.ScreencastCollection = new ObservableCollection<IScreencastViewModel>();
            foreach (var item in this.GetAllScreencast())
            {
                item.ScreencastEnded += new EventHandler<EventArgs>(this.OnScreencastEnded);
                this.ScreencastCollection.Add(item);
            }
            // Initialisation des commandes
        }

        void OnScreencastEnded(object sender, EventArgs e)
        {
            if (this.ScreecastIndex < (this.ScreencastCollection.Count - 1))
            {
                this.ScreecastIndex++;
                this.ScreencastCollection[this.ScreecastIndex].View.IsPlaying = true;
            }
            else
            {
                this.ScreecastIndex = 0;
                this.ScreencastCollection[this.ScreecastIndex].View.IsPlaying = false;
            }
        }

        #endregion

        #region GetAllScreencast Method

        /// <summary>
        /// Cette méthode permet de récupérer l'ensemble des vidéo cast saisie dans le fichier de Configuration xml
        /// </summary>
        /// <returns>Enumération de ScreencastViewModel</returns>
        private IEnumerable<IScreencastViewModel> GetAllScreencast()
        {
            var screencasts = ConfigurationManager.Instance.AppSettings["Screencasts"];
            if (!String.IsNullOrEmpty(screencasts))
            {
                var splitScreenCasts = screencasts.Split(';');
                foreach (var item in splitScreenCasts)
                {
                    if (!string.IsNullOrWhiteSpace(item)) // Vérifier la bonne valeur avec affichage !
                    {
                        var itemViewModel = this.container.Resolve<IScreencastViewModel>();
                        itemViewModel.FileName = new Uri(item, UriKind.RelativeOrAbsolute);
                        yield return itemViewModel;
                    }
                }
            }
        } 

        #endregion
    }

}
